home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SpriteEngine / SE No Timer / SpriteHanders.c < prev    next >
C/C++ Source or Header  |  1995-03-12  |  4KB  |  141 lines

  1. #include "SpriteTools.h"
  2.  
  3. // SpriteTools.h includes SpriteHandlers.h
  4.  
  5.  
  6. /*** Custom handlers - application dependent ***
  7.  
  8. Edit this as necessary. It should always include the following three routines:
  9.  
  10. MoveSprite: move the sprite
  11.  
  12. HitSprite: handle collisions between two sprites
  13.  
  14. InitSprites: Load all faces and create initial sprites
  15.  
  16. ***/
  17.  
  18.  
  19. GrafPtr    firstFace, secondFace, thirdFace;
  20.  
  21.  
  22. void MoveSprite(SpritePtr theSprite)
  23. {
  24.     theSprite->speed.v++; // Simple gravity
  25.     theSprite->fixedPointPosition.h += theSprite->speed.h;
  26.     theSprite->fixedPointPosition.v += theSprite->speed.v;
  27.     theSprite->position.h = theSprite->fixedPointPosition.h >> 4;
  28.     theSprite->position.v = theSprite->fixedPointPosition.v >> 4;
  29.     KeepOnScreenFixed(theSprite);
  30. } /*MoveSprite*/
  31.  
  32.  
  33. void HitSprite(SpritePtr theSprite, SpritePtr anotherSprite)
  34. {
  35.     short    tempSpeed;
  36.     
  37.     if (RectSeparate(theSprite, anotherSprite) >= 2) // 2 or 3: horizontal, otherwise vertical
  38.     {
  39.         tempSpeed = theSprite->speed.h;
  40.         theSprite->speed.h = anotherSprite->speed.h;
  41.         anotherSprite->speed.h = tempSpeed;
  42.         theSprite->fixedPointPosition.h = theSprite->position.h << 4;
  43.     }
  44.     else
  45.     {
  46.         tempSpeed = theSprite->speed.v;
  47.         theSprite->speed.v = anotherSprite->speed.v;
  48.         anotherSprite->speed.v = tempSpeed;
  49.         theSprite->fixedPointPosition.v = theSprite->position.v << 4;
  50.     }
  51.  
  52. } /*HitSprite*/
  53.  
  54.  
  55. void InitSprites()
  56. {
  57.     SpritePtr    theSprite;
  58.  
  59. /*Load all pictures*/
  60.     firstFace = LoadFaceFromCicn(128);            /*cicn resource #128.*/
  61.     secondFace = LoadFaceFromCicn(129);            /*cicn resource #129.*/
  62.     thirdFace = LoadFaceFromCicn(130);            /*cicn resource #130.*/
  63.  
  64. /*Create sprites*/
  65.     theSprite = NewSprite();
  66.     theSprite->face = firstFace;
  67.     SetPt(&theSprite->fixedPointPosition, 100 << 4, 100 << 4);
  68.     SetPt(&theSprite->speed, Rand(7)-3, Rand(7)-3);
  69.     theSprite->lastTime = TickCount();
  70.  
  71.     theSprite = NewSprite();
  72.     theSprite->face = secondFace;
  73.     SetPt(&theSprite->fixedPointPosition, 50 << 4, 50 << 4);
  74.     SetPt(&theSprite->speed, Rand(7)-3, Rand(7)-3);
  75.     theSprite->lastTime = TickCount();
  76.  
  77.     theSprite = NewSprite();
  78.     theSprite->face = thirdFace;
  79.     SetPt(&theSprite->fixedPointPosition, 150 << 4, 150 << 4);
  80.     SetPt(&theSprite->speed, Rand(7)-3, Rand(7)-3);
  81.     theSprite->lastTime = TickCount();
  82.  
  83.  
  84. // extra
  85.  
  86.     theSprite = NewSprite();
  87.     theSprite->face = firstFace;
  88.     SetPt(&theSprite->fixedPointPosition, 100 << 4, 100 << 4);
  89.     SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
  90.     theSprite->lastTime = TickCount();
  91.  
  92.     theSprite = NewSprite();
  93.     theSprite->face = secondFace;
  94.     SetPt(&theSprite->fixedPointPosition, 50 << 4, 50 << 4);
  95.     SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
  96.     theSprite->lastTime = TickCount();
  97.  
  98.     theSprite = NewSprite();
  99.     theSprite->face = thirdFace;
  100.     SetPt(&theSprite->fixedPointPosition, 150 << 4, 150 << 4);
  101.     SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
  102.     theSprite->lastTime = TickCount();
  103.  
  104.     theSprite = NewSprite();
  105.     theSprite->face = firstFace;
  106.     SetPt(&theSprite->fixedPointPosition, 100 << 4, 100 << 4);
  107.     SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
  108.     theSprite->lastTime = TickCount();
  109.  
  110.     theSprite = NewSprite();
  111.     theSprite->face = secondFace;
  112.     SetPt(&theSprite->fixedPointPosition, 50 << 4, 50 << 4);
  113.     SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
  114.     theSprite->lastTime = TickCount();
  115.  
  116.     theSprite = NewSprite();
  117.     theSprite->face = thirdFace;
  118.     SetPt(&theSprite->fixedPointPosition, 150 << 4, 150 << 4);
  119.     SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
  120.     theSprite->lastTime = TickCount();
  121.  
  122.     theSprite = NewSprite();
  123.     theSprite->face = firstFace;
  124.     SetPt(&theSprite->fixedPointPosition, 100 << 4, 100 << 4);
  125.     SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
  126.     theSprite->lastTime = TickCount();
  127.  
  128.     theSprite = NewSprite();
  129.     theSprite->face = secondFace;
  130.     SetPt(&theSprite->fixedPointPosition, 50 << 4, 50 << 4);
  131.     SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
  132.     theSprite->lastTime = TickCount();
  133.  
  134.     theSprite = NewSprite();
  135.     theSprite->face = thirdFace;
  136.     SetPt(&theSprite->fixedPointPosition, 150 << 4, 150 << 4);
  137.     SetPt(&theSprite->speed, Rand(15)-7, Rand(7)-3);
  138.     theSprite->lastTime = TickCount();
  139.  
  140. } /*InitSprites*/
  141.